package com.group.transaction.service.permission.impl;

import com.alibaba.fastjson.JSONObject;
import com.group.transaction.domain.Permission;
import com.group.transaction.mapper.PermissionMapper;
import com.group.transaction.model.ResultJson;
import com.group.transaction.service.permission.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author YunXing Pang
 * @date 2020/4/2
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Set<String> selectValuesByUsername(String username) {
        return null;
    }

    /**
     * 查询权限信息
     * @return
     */
    @Override
    public JSONObject queryAllPermissionData() {
        List<Permission> permissionList = permissionMapper.selectAll();
        if(permissionList == null || permissionList.size() == 0) {
            return new JSONObject().fluentPut("result", null);
        }
        return new JSONObject().fluentPut("result", disposePermissionData(permissionList));
    }

    /**
     * 查询权限父节点
     * @param permissionList
     * @return
     */
    public List<Map<String,Object>> disposePermissionData(List<Permission> permissionList) {
        List<Map<String,Object>> list = new ArrayList<>();
        for(Permission p: permissionList) {
            if(p.getPid() == null) {
                Map<String,Object> map = new HashMap<>();
                map.put("name",p.getName());
                map.put("id",p.getId());
                map.put("label",p.getName());
                map.put("value",p.getId());
                List<Map<String,Object>> childrenList = permissionNode(permissionList,p.getId());
                if(childrenList != null && childrenList.size() > 0) {
                    map.put("children",childrenList);
                }
                list.add(map);
            }
        }
        return  list;
    }

    /**
     * 递归查询权限子节点
     * @param permissionList
     * @param pid
     * @return
     */
    public List<Map<String,Object>> permissionNode(List<Permission> permissionList,Long pid) {
        List<Map<String,Object>> list = new ArrayList<>();
        for(Permission p: permissionList) {
            if(p.getPid() != null && p.getPid().equals(pid)) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",p.getId());
                map.put("name",p.getName());
                map.put("label",p.getName());
                map.put("value",p.getId());
                List<Map<String,Object>> childrenList = permissionNode(permissionList,p.getId());
                if(childrenList != null && childrenList.size() > 0) {
                    map.put("children",childrenList);
                }
                list.add(map);
            }
        }
        return list;
    }
    /**
     * 根据角色id查询权限
     * @return
     */
    @Override
    public JSONObject queryPermissionByRoleId(Integer roleId){
        return new JSONObject().fluentPut("result", permissionMapper.queryPermissionByRoleId(roleId));
    }
}
